package com.vcourse.service.edu.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.vcourse.commons.result.R;
import com.vcourse.commons.utils.PageUtils;
import com.vcourse.service.edu.entity.Teacher;
import com.vcourse.service.edu.service.TeacherService;
import com.vcourse.service.edu.vo.TeacherQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Zengsh123
 * @since 2020-05-25
 */

@Api(tags = "讲师接口")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("/listAll")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data(list);
    }

    @ApiOperation("分页查询讲师")
    @GetMapping("/list/{currentPage}/{limit}")
    public R listPage(@ApiParam("当前页码") @PathVariable Long currentPage,
                      @ApiParam("每页条数") @PathVariable Long limit,
                      @ApiParam("查询对象") TeacherQueryVo queryVo) {
        PageUtils listPage = teacherService.queryPage(currentPage, limit, queryVo);
        return R.ok().data(listPage);
    }

    @ApiOperation("根据ID查讲师")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam("讲师ID") @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data(teacher);
        }
        return R.ok().message("无此讲师信息");
    }

    @ApiOperation("新增讲师信息")
    @PostMapping("/add")
    public R add(@ApiParam("讲师信息") @RequestBody Teacher teacher) {
        teacherService.save(teacher);
        return R.ok().message("添加成功");
    }

    @ApiOperation("修改讲师信息")
    @PutMapping("/update")
    public R updateById(@ApiParam("讲师信息") @RequestBody Teacher teacher) {
        boolean res = teacherService.updateById(teacher);
        if (res) {
            return R.ok().message("修改成功");
        }
        return R.ok().message("修改失败");
    }

    @ApiOperation("删除讲师")
    @DeleteMapping("/remove/{id}")
    public R remove(@ApiParam("讲师id") @PathVariable String id) {
        // 删除头像文件
        teacherService.deleteAvatarByIds(CollUtil.newArrayList(id));
        boolean res = teacherService.removeById(id);
        if (res) {
            return R.ok().message("删除成功");
        }
        return R.ok().message("删除失败");
    }

    @ApiOperation("批量删除讲师")
    @DeleteMapping("/batchRemove")
    public R batchRemove(@ApiParam("讲师id列表")
                         @RequestBody List<String> ids) {
        // 删除头像文件
        teacherService.deleteAvatarByIds(ids);

        boolean res = teacherService.removeByIds(ids);
        if (res) {
            return R.ok().message("删除成功");
        }
        return R.ok().message("删除失败");
    }

    @ApiOperation("根据姓名进行模糊查询，给搜索框提示")
    @GetMapping("/getNames/{name}")
    public R getNames(@ApiParam("姓名前几个字") @PathVariable String name) {
        List<Map<String, Object>> nameList = teacherService.getNames(name);
        return R.ok().data(nameList);
    }

}

